Merge "rdbms: make LBFactory close/rollback dangling handles like LoadBalancer"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
index 522af43..d239ac1 100644 (file)
@@ -19,9 +19,10 @@ class BagOStuffTest extends MediaWikiTestCase {
 
                // type defined through parameter
                if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
-                       $name = $this->getCliArg( 'use-bagostuff' );
+                       global $wgObjectCaches;
 
-                       $this->cache = ObjectCache::newFromId( $name );
+                       $id = $this->getCliArg( 'use-bagostuff' );
+                       $this->cache = ObjectCache::newFromParams( $wgObjectCaches[$id] );
                } else {
                        // no type defined - use simple hash
                        $this->cache = new HashBagOStuff;
@@ -36,7 +37,7 @@ class BagOStuffTest extends MediaWikiTestCase {
         * @covers MediumSpecificBagOStuff::makeKeyInternal
         */
        public function testMakeKey() {
-               $cache = ObjectCache::newFromId( 'hash' );
+               $cache = new HashBagOStuff();
 
                $localKey = $cache->makeKey( 'first', 'second', 'third' );
                $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
@@ -112,21 +113,51 @@ class BagOStuffTest extends MediaWikiTestCase {
        /**
         * @covers MediumSpecificBagOStuff::changeTTL
         */
-       public function testChangeTTL() {
+       public function testChangeTTLRenew() {
+               $now = microtime( true ); // need real time
+               $this->cache->setMockTime( $now );
+
                $key = $this->cache->makeKey( self::TEST_KEY );
                $value = 'meow';
 
-               $this->cache->add( $key, $value, 5 );
+               $this->cache->add( $key, $value, 60 );
                $this->assertEquals( $value, $this->cache->get( $key ) );
-               $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
-               $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
                $this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
                $this->assertEquals( $this->cache->get( $key ), $value );
+
                $this->cache->delete( $key );
                $this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
+       }
+
+       /**
+        * @covers MediumSpecificBagOStuff::changeTTL
+        */
+       public function testChangeTTLExpireRel() {
+               $now = microtime( true ); // need real time
+               $this->cache->setMockTime( $now );
+
+               $key = $this->cache->makeKey( self::TEST_KEY );
+               $value = 'meow';
 
                $this->cache->add( $key, $value, 5 );
-               $this->assertTrue( $this->cache->changeTTL( $key, time() - 3600 ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, -3600 ) );
+               $this->assertFalse( $this->cache->get( $key ) );
+       }
+
+       /**
+        * @covers MediumSpecificBagOStuff::changeTTL
+        */
+       public function testChangeTTLExpireAbs() {
+               $now = microtime( true ); // need real time
+               $this->cache->setMockTime( $now );
+
+               $key = $this->cache->makeKey( self::TEST_KEY );
+               $value = 'meow';
+
+               $this->cache->add( $key, $value, 5 );
+               $this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );
                $this->assertFalse( $this->cache->get( $key ) );
        }
 
@@ -134,16 +165,16 @@ class BagOStuffTest extends MediaWikiTestCase {
         * @covers MediumSpecificBagOStuff::changeTTLMulti
         */
        public function testChangeTTLMulti() {
+               $now = 1563892142;
+               $this->cache->setMockTime( $now );
+
                $key1 = $this->cache->makeKey( 'test-key1' );
                $key2 = $this->cache->makeKey( 'test-key2' );
                $key3 = $this->cache->makeKey( 'test-key3' );
                $key4 = $this->cache->makeKey( 'test-key4' );
 
                // cleanup
-               $this->cache->delete( $key1 );
-               $this->cache->delete( $key2 );
-               $this->cache->delete( $key3 );
-               $this->cache->delete( $key4 );
+               $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
 
                $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
                $this->assertFalse( $ok, "No keys found" );
@@ -152,7 +183,6 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->assertFalse( $this->cache->get( $key3 ) );
 
                $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
-
                $this->assertTrue( $ok, "setMulti() succeeded" );
                $this->assertEquals(
                        3,
@@ -166,21 +196,24 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->assertEquals( 2, $this->cache->get( $key2 ) );
                $this->assertEquals( 3, $this->cache->get( $key3 ) );
 
-               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], time() + 86400 );
-               $this->assertTrue( $ok, "Expiry set for all keys" );
-
                $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
                $this->assertFalse( $ok, "One key missing" );
+               $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
+
+               $now = microtime( true ); // real time
+               $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
+               $this->assertTrue( $ok, "setMulti() succeeded" );
+
+               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
+               $this->assertTrue( $ok, "Expiry set for all keys" );
+               $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
 
                $this->assertEquals( 2, $this->cache->incr( $key1 ) );
                $this->assertEquals( 3, $this->cache->incr( $key2 ) );
                $this->assertEquals( 4, $this->cache->incr( $key3 ) );
 
                // cleanup
-               $this->cache->delete( $key1 );
-               $this->cache->delete( $key2 );
-               $this->cache->delete( $key3 );
-               $this->cache->delete( $key4 );
+               $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
        }
 
        /**
@@ -210,17 +243,29 @@ class BagOStuffTest extends MediaWikiTestCase {
         * @covers MediumSpecificBagOStuff::getWithSetCallback
         */
        public function testGetWithSetCallback() {
-               $key = $this->cache->makeKey( self::TEST_KEY );
-               $value = $this->cache->getWithSetCallback(
+               $now = 1563892142;
+               $cache = new HashBagOStuff( [] );
+               $cache->setMockTime( $now );
+               $key = $cache->makeKey( self::TEST_KEY );
+
+               $this->assertFalse( $cache->get( $key ), "No value" );
+
+               $value = $cache->getWithSetCallback(
                        $key,
                        30,
-                       function () {
+                       function ( &$ttl ) {
+                               $ttl = 10;
+
                                return 'hello kitty';
                        }
                );
 
                $this->assertEquals( 'hello kitty', $value );
-               $this->assertEquals( $value, $this->cache->get( $key ) );
+               $this->assertEquals( $value, $cache->get( $key ), "Value set" );
+
+               $now += 11;
+
+               $this->assertFalse( $cache->get( $key ), "Value expired" );
        }
 
        /**
@@ -245,6 +290,10 @@ class BagOStuffTest extends MediaWikiTestCase {
 
                $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
                $this->assertEquals( 4, $val, "Correct init value" );
+               $this->cache->delete( $key );
+
+               $val = $this->cache->incrWithInit( $key, 0, 5 );
+               $this->assertEquals( 5, $val, "Correct init value" );
        }
 
        /**
@@ -336,24 +385,39 @@ class BagOStuffTest extends MediaWikiTestCase {
                        return $oldValue . '!';
                };
 
-               foreach ( [ $tiny, $small, $big ] as $value ) {
+               $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
+               foreach ( $cases as $case => $value ) {
                        $this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
-                       $this->assertEquals( $value, $this->cache->get( $key ) );
-                       $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key] );
-
-                       $this->assertTrue( $this->cache->merge( $key, $callback, 5 ) );
-                       $this->assertEquals( "$value!", $this->cache->get( $key ) );
-                       $this->assertEquals( "$value!", $this->cache->getMulti( [ $key ] )[$key] );
-
-                       $this->assertTrue( $this->cache->deleteMulti( [ $key ] ) );
-                       $this->assertFalse( $this->cache->get( $key ) );
-                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+                       $this->assertEquals( $value, $this->cache->get( $key ), "get $case" );
+                       $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key], "get $case" );
+
+                       $this->assertTrue(
+                               $this->cache->merge( $key, $callback, 5, 1, BagOStuff::WRITE_ALLOW_SEGMENTS ),
+                               "merge $case"
+                       );
+                       $this->assertEquals(
+                               "$value!",
+                               $this->cache->get( $key ),
+                               "merged $case"
+                       );
+                       $this->assertEquals(
+                               "$value!",
+                               $this->cache->getMulti( [ $key ] )[$key],
+                               "merged $case"
+                       );
+
+                       $this->assertTrue( $this->cache->deleteMulti( [ $key ] ), "delete $case" );
+                       $this->assertFalse( $this->cache->get( $key ), "deleted $case" );
+                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "deletd $case" );
 
                        $this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
-                       $this->assertEquals( "@$value", $this->cache->get( $key ) );
-                       $this->assertTrue( $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ) );
-                       $this->assertFalse( $this->cache->get( $key ) );
-                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+                       $this->assertEquals( "@$value", $this->cache->get( $key ), "get $case" );
+                       $this->assertTrue(
+                               $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ),
+                               "prune $case"
+                       );
+                       $this->assertFalse( $this->cache->get( $key ), "pruned $case" );
+                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "pruned $case" );
                }
 
                $this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );